package com.shiyipicture.shiyipicturebackend.controller;

import com.shiyipicture.shiyipicturebackend.common.BaseResponse;
import com.shiyipicture.shiyipicturebackend.common.ResultUtils;
import com.shiyipicture.shiyipicturebackend.exception.ErrorCode;
import com.shiyipicture.shiyipicturebackend.exception.ThrowUtils;
import com.shiyipicture.shiyipicturebackend.model.dto.space.analyze.*;
import com.shiyipicture.shiyipicturebackend.model.entity.Space;
import com.shiyipicture.shiyipicturebackend.model.vo.space.analyze.*;
import com.shiyipicture.shiyipicturebackend.service.SpaceAnalyzeService;
import com.shiyipicture.shiyipicturebackend.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/space/analyze")
public class SpaceAnalyzeController {
  @Resource
  private SpaceAnalyzeService spaceAnalyzeService;

  @Resource
  private UserService userService;

  /**
   * 获取空间使用状态
   * @param spaceUsageAnalyzeRequest
   * @param request
   * @return
   */
  @PostMapping("/usage")
  public BaseResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeRequest spaceUsageAnalyzeRequest,
                                                                      HttpServletRequest request){
    ThrowUtils.throwIf(spaceUsageAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
    SpaceUsageAnalyzeResponse spaceUsageAnalyzeList = spaceAnalyzeService.getSpaceUsageAnalyze(spaceUsageAnalyzeRequest, userService.getLoginUser(request));
    return ResultUtils.success(spaceUsageAnalyzeList);
  }

  /**
   * 获取空间使用状态
   * @param spaceUserAnalyzeRequest
   * @param request
   * @return
   */
  @PostMapping("/user")
  public BaseResponse<List<SpaceUserAnalyzeResponse>> getSpaceUserAnalyze(@RequestBody SpaceUserAnalyzeRequest spaceUserAnalyzeRequest,
                                                                     HttpServletRequest request){
    ThrowUtils.throwIf(spaceUserAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
    List<SpaceUserAnalyzeResponse> spaceUserAnalyzeList = spaceAnalyzeService.getSpaceUserAnalyze(spaceUserAnalyzeRequest, userService.getLoginUser(request));
    return ResultUtils.success(spaceUserAnalyzeList);
  }

  /**
   * 获取图片分类分析数据
   * @param spaceCategoryAnalyzeRequest
   * @param request
   * @return
   */
  @PostMapping("/category")
  public BaseResponse<List<SpaceCategoryAnalyzeResponse>> getSpaceCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeRequest spaceCategoryAnalyzeRequest,
                                                                                  HttpServletRequest request){
    ThrowUtils.throwIf(spaceCategoryAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
    List<SpaceCategoryAnalyzeResponse> spaceCategoryAnalyzeList = spaceAnalyzeService.getSpaceCategoryAnalyze(spaceCategoryAnalyzeRequest, userService.getLoginUser(request));
    return ResultUtils.success(spaceCategoryAnalyzeList);
  }

  /**
   * 获取图片标签分析数据
   * @param spaceTagAnalyzeRequest
   * @param request
   * @return
   */
  @PostMapping("/tag")
  public BaseResponse<List<SpaceTagAnalyzeResponse>> getSpaceTagAnalyze(@RequestBody SpaceTagAnalyzeRequest spaceTagAnalyzeRequest,
                                                                        HttpServletRequest request){
    ThrowUtils.throwIf(spaceTagAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
    List<SpaceTagAnalyzeResponse> spaceTagAnalyzeList = spaceAnalyzeService.getSpaceTagAnalyze(spaceTagAnalyzeRequest, userService.getLoginUser(request));
    return ResultUtils.success(spaceTagAnalyzeList);
  }

  /**
   * 分析图片大小范围及其数量
   * @param spaceSizeAnalyzeRequest
   * @param request
   * @return
   */
  @PostMapping("/size")
  public BaseResponse<List<SpaceSizeAnalyzeResponse>> getSpaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeRequest spaceSizeAnalyzeRequest,
                                                                          HttpServletRequest request){
    ThrowUtils.throwIf(spaceSizeAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
    List<SpaceSizeAnalyzeResponse> spaceSizeAnalyzeList = spaceAnalyzeService.getSpaceSizeAnalyze(spaceSizeAnalyzeRequest, userService.getLoginUser(request));
    return ResultUtils.success(spaceSizeAnalyzeList);
  }

  /**
   * 获取空间排行
   * @param spaceRankAnalyzeRequest
   * @param request
   * @return
   */
  @PostMapping("/rank")
  public BaseResponse<List<Space>> getSpaceRank(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest, HttpServletRequest request){
    ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR);
    List<Space> spaceRankList = spaceAnalyzeService.getSpaceRankAnalyze(spaceRankAnalyzeRequest, userService.getLoginUser(request));
    return ResultUtils.success(spaceRankList);
  }
}
